@extends('layouts.default')

@section('title', '社会实践')

@push('scripts')
<script src="{{ asset('lib/xm-select-v1.2.4/xm-select.js')}}"></script>
<script>
    layui.use(['layer','form','table','laytpl','laydate','element','laypage','upload'], function(){
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var laytpl = layui.laytpl;
        var laydate = layui.laydate;
        var element = layui.element;
        var laypage = layui.laypage;
        var upload = layui.upload;
        var xmDemo;
        var PageId = "{{request()->get('id',0)}}";
        var clFiles = [];
        
        renderSelect();

        laydate.render({
            elem: '#show-date'
            ,type: 'date'
        });

        //下拉框初始化
        async function renderSelect(){
            await renderDictionarySelect();
            await renderTermSelect();
            await renderParticipants();
            pageInit();
        }

        //字典下拉框数据渲染
        function renderDictionarySelect() {
            return new Promise((resolve) => {
                getDictionaryItem({'diccode': ['LEVEL']}, function (res){
                    if(res.code === 1) {  
                        let data = res.data;

                        for(var k in data) {
                            let key = k.toLowerCase();
                            let d = data[k];
                            
                            $('[data-role='+key+'List]').empty().append('<option value="">请选择</option>');
                            for (var i in d) {
                                $('[data-role='+key+'List]').append('<option value="' + d[i].optionId + '">' + d[i].selectValue + '</option>');
                            }
                        }
                        
                        form.render(); 
                        
                        resolve();
                    }
                });
            });
        }

        //学期下拉框数据渲染
        function renderTermSelect() {
            return new Promise((resolve) => {
                getTermData({},function(res){
                    if(res.code == 1){
                        $('[data-role=termList]').empty().append('<option value="">请选择</option>');
                        for(var i in res.data){
                            $('[data-role=termList]').append('<option value="'+res.data[i].id+'">'+res.data[i].title+'</option>');
                        }
                                    
                        form.render('select');

                        resolve();
                    }
                });  
            });
        } 

        //参与教师下拉框数据渲染
        function renderParticipants() {
            return new Promise((resolve) => {
                getParticipantsData({},function(res){
                    if(res.code == 1){
                        xmDemo = xmSelect.render({
                            el: '#pter', 
                            autoRow: true,
                            filterable: true,
                            tree: {
                                show: true,
                                showFolderIcon: true,
                                showLine: true,
                                indent: 20,
                                expandedKeys:false
                            },   
                            direction: 'down',           
                            filterable: true,
                            height: 'auto',
                            name:'participant',
                            layVerify: 'required',
                            layVerType: 'msg',
                            layReqText:'参与教师不能为空',
                            theme: {
                                color: '#2d8cf0',
                            },
                            searchTips:'关键字搜索',
                            data: function(){
                                return res.data
                            }
                        })

                        return resolve();
                    }
                });  
            });
        }

        //数据渲染
        function pageInit(){
            getActivityOne({id:PageId}, function(res) {
                if(res.code === 1) {
                    let Activity = res.data;

                    form.val('editForm', {
                        'id': PageId,
                        'data_version_id': Activity.data_version_id,
                        'name': Activity.name,
                        'date': Activity.date,
                        'level': Activity.level_array[0].optionId
                    });

                    //参与教师
                    xmDemo.setValue(Activity.participants_users.pant);
                    //文件                     
                    if(Activity.proof_material_array.length){
                        layui.each(Activity.proof_material_array, function(i, d) {
                            if(d.field == 'cl') {
                                clFiles.push(d.file_origin);
                                $('#cl-file').val(JSON.stringify(clFiles));

                                $('#cl-uploader').parent().find('.file-show').html('');
                                for(i in clFiles) {
                                    if(clFiles[i]) {
                                        let fsHtml = '<div class="file-item"><span class="file-name">'+clFiles[i].file_name+'</span><span class="layui-icon layui-icon-close-fill" onClick="fileDelete('+i+', 1)"></span></div>';
                                        $('#cl-uploader').parent().find('.file-show').append(fsHtml); 
                                    }
                                }
                            }
                        });
                    } 
                } else {
                    layer.msg('无数据记录', {time: 2000, icon: 5});
                }
            });
        }

        window.fileDelete = function (fk, ft) {
            if(ft == 1) {
                delete clFiles[fk];
                let files = clFiles;
                
                $('#cl-file').val(JSON.stringify(clFiles));
                $('#cl-uploader').parent().find('.file-show').html('');
                for(i in files) {
                    if(files[i]) {
                        let fsHtml = '<div class="file-item"><span class="file-name">'+files[i].file_name+'</span><span class="layui-icon layui-icon-close-fill" onClick="fileDelete('+i+', 1)"></span></div>';
                        $('#cl-uploader').parent().find('.file-show').append(fsHtml);
                    }
                }
            }
        }

        //文件上传
        upload.render({
            elem: '#cl-uploader'
            ,url: "{{route('uploads.upload')}}" //此处配置你自己的上传接口即可
            ,accept: 'file' //普通文件
            ,multiple: true
            ,allDone: function(obj){ //当文件全部被提交后，才触发
                if(obj.total == obj.successful) {
                    layer.msg('上传成功');
                } else {
                    layer.msg(obj.failed + '个文件上传失败！');
                }
            }
            ,done: function(res){
                clFiles.push(res.data);
                $('#cl-file').val(JSON.stringify(clFiles));

                $('#cl-uploader').parent().find('.file-show').html('');
                for(i in clFiles) {
                    if(clFiles[i]) {
                        let fsHtml = '<div class="file-item"><span class="file-name">'+clFiles[i].file_name+'</span><span class="layui-icon layui-icon-close-fill" onClick="fileDelete('+i+', 1)"></span></div>';
                        $('#cl-uploader').parent().find('.file-show').append(fsHtml); 
                    }
                }
            }
            ,error: function(index, upload){ //错误回调
                console.log(index);
            }
        });

        //提交
        form.on('submit(sub)', function(data){
            let d = data.field;
            d.participants = xmDemo.getValue('nameStr');
            d.participant_ids = xmDemo.getValue('value');
            d.do_task = 1;

            editActivity(d, function(res){
                if(res.code == '1'){
                    layer.msg(res.msg,{icon: 6,time: 1000},function(){
                        window.history.back();
                    });
                }else{
                    layer.msg(res.msg, {icon: 5,shift : 6});
                }
            });

            return false;
        });
    });
</script>
@endpush

@section('content')
<style>
    .opt-panel{padding: 10px;}
    .panel-title h3{margin-bottom: 20px;font-weight: 800;}

    .opt-form{padding: 20px 5px;}
    .opt-area{display: flex;flex-direction: row-reverse;padding: 10px 20px;margin-bottom: 20px;}
    .opt-area button, .back-btn{margin-left: 10px;}
</style>

<div class="layui-row">
    <div class="layui-panel opt-panel">
        <form class="layui-form opt-form" action="" id="edit-form" lay-filter="editForm">
            <div class="layui-row">
                <div class="panel-title">
                    <h3>社会实践 - 编辑</h3>
                </div>
                <div class="layuik-form-item layuik-columns-two">
                    <div class="layuik-inline">
                        <label class="layuik-form-label"><i class="redstar">*</i>学年学期：</label>
                        <div class="layuik-input-inline">
                            <select class="data-version-id" name="data_version_id" lay-filter="termList" data-role="termList" lay-verify="required" lay-reqText="不能为空"></select>
                        </div>
                    </div>
                    <div class="layuik-inline">
                        <label class="layuik-form-label"><i class="redstar">*</i>名称：</label>
                        <div class="layuik-input-inline">
                            <input type="text" name="name" required lay-verify="required" placeholder="请输入" lay-reqText="不能为空" autocomplete="off" class="layui-input">
                        </div>
                    </div>
                </div> 
                <div class="layuik-form-item layuik-columns-two">
                    <div class="layuik-inline">
                        <label class="layuik-form-label"><i class="redstar">*</i>级别：</label>
                        <div class="layuik-input-inline">
                            <select name="level" lay-filter="levelList" data-role="levelList" lay-verify="required" lay-reqText="不能为空"></select>
                        </div>
                    </div>
                    <div class="layuik-inline">
                        <label class="layuik-form-label"><i class="redstar">*</i>日期：</label>
                        <div class="layuik-input-inline">
                            <input type="text" name="date" id="show-date" required lay-verify="required" placeholder="请选择" lay-reqText="不能为空" autocomplete="off" class="layui-input">
                        </div>
                    </div>
                </div>
                <div class="layuik-form-item">
                    <div class="layuik-inline">
                        <label class="layuik-form-label"><i class="redstar">*</i>参与教师：</label>
                        <div class="layuik-input-inline">
                            <div type="text" id="pter" class="xm-select-demo"></div>
                        </div>
                    </div>
                </div>
                <div class="layuik-form-item layuik-columns-two">
                    <div class="layuik-inline">
                        <label class="layuik-form-label">证明材料：</label>
                        <div class="layuik-input-inline layuik-upload">
                            <button type="button" class="layui-btn layui-btn-normal" id="cl-uploader"><i class="layui-icon"></i>上传文件</button>
                            <div class="file-show"></div>
                            <input style="display:none" type="text" id="cl-file" name="files[cl]" value="">
                        </div>
                    </div>
                </div>
            </div>
            
            <div class="layui-row">
                <div class="opt-area">
                    <a href="javascript:history.go(-1);" class="layui-btn layui-btn-primary back-btn">返回</a>
                    <button class="layui-btn layui-btn-normal" lay-submit lay-filter="sub">提交</button>
                    <input type="hidden" name="activity_category" value="EW_SOCIAL_PRACTICE_TABLE">
                    <input type="hidden" name="id" value="">
                </div> 
            </div>
        </form>
    </div>   
</div>
  
@endsection